<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="plusQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="dicTypes" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" @row-dblclick="rowDbClick">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="120">
      </el-table-column>
      <el-table-column prop="name" label="数据字典名" width="250"  sortable>
      </el-table-column>
      <el-table-column prop="sn" label="编号" width="250" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--下方工具条-->
    <el-col :span="24" class="toolbar">
      <!--  这里批量删除使用了disabled属性设置,只有选中时才能点击批量删除,原理就是选中行会把行数据以数组的形式返回,有长度才表示选中    -->
      <el-button type="danger" @click="batchRemove"
                 :disabled="this.sels.length===0">批量删除</el-button>
      <!--2.4 在分页工具条这里,把总条数以及pageSize传到里面的属性   -->
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :current-page="currentPage"
                     :total="total"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面
     :rules  申明表单校验规则
     ref    给当前表单取一个引用别名
   -->
    <el-dialog :title="myTitles" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
        <el-form-item label="数据字典名" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>


<!--  点击抽屉
-->
    <div>
      <el-drawer
          title="该产品类型下商品明细!"
          :visible.sync="table"
          direction="rtl"
          size="50%">
        <el-table :data="details">
          <el-table-column property="name" label="产品名称" width="350"></el-table-column>
        </el-table>

      </el-drawer>
    </div>

  </section>

</template>

<script>

export default {
  data() {
    return {
      myTitles:"",
      table: false,
      details:[],
      dicTypes:[],
      filters: {
        keyword: ''//关键字查询,高级查询
      },
      total: 0,
      pageSize: 5,
      currentPage : 1,
      types_id : null,
      listLoading: false,
      sels: [],//列表选中列,批量查询的参数,会传回当前页所有选中行的数据

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [
          { required: true, message: '请输入字典名', trigger: 'blur' }
        ],
        sn: [//这里设置新增修改必填项
          { required: true, message: '请输入编号', trigger: 'blur' }
        ],
      },
      //编辑界面数据
      editForm: {
        id: null,
        sn:"",
        name: ""
      },
    }
  },
  methods: {
    //获取到产品明细
    rowDbClick(row,column,event){
      row.id;
      this.table=true;
      let param = {
        currentPage : this.currentPage,
        pageSize : this.pageSize,
        types_id : row.id,
      };
      this.$http.post("/detail/more",param)
          .then(result=>{
            result = result.data;
            this.details = result.rows;
            this.listLoading = false;
          })
          .catch(result=>{
            console.log(result);
            this.$message({
              message : "系统繁忙,请联系程序员",
              type : "error"
            });
          })
    },

    //2.设置高级查询
    //2.3 把这个方法绑定给查询按钮
    plusQuery(){
      //2.1 设置查询的定位要从第一条数据开始
      this.currentPage = 1;
      this.getDictionnaryTypes();
    },
    //2.5 做好高级查询以后,要对分页工具条进行设置,设置当前页码发生改变时,即传回这个改变的数给currentPage
    handleCurrentChange(val) {
      this.currentPage = val;
      console.log(val)
      this.getDictionnaryTypes();
    },


    //获取产品类型列表
    getDictionnaryTypes() {
      let param = {
        currentPage : this.currentPage,
        pageSize : this.pageSize,
        keyword: this.filters.keyword
      };
      //1.1 加载完页面后,开启忙等
      this.listLoading = true;
      this.$http.post("/dictype",param)
          .then(result=>{
            result = result.data;
            this.dicTypes = result.rows;
            this.total = result.total;
            this.listLoading = false;
          })
          .catch(result=>{
            console.log(result);
            this.$message({
              message : "系统繁忙,请联系程序员",
              type : "error"
            });
          })
    },

    //-3.删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      })
          .then(() => {
            this.listLoading = true;
            //3.1 发送删除的axios请求,这里直接在url中写一个字符串拼接,吧行的id直接拼接上去row.id,不传参数
            this.$http.delete("/dictype/"+row.id)
                .then(result => {
                  this.listLoading = false;
                  result = result.data;//这里会拿到AjaxResult
                  if(!result.success){
                    this.$message({
                      //3.2在工具类的ajaxresult中已经配置了信息,这里直接用
                      message: result.message,
                      type: 'error'
                    });
                  }
                  //3.3 删除以后定位到第一页,定位以后再执行查询,吧数据展示出来
                  this.currentPage = 1;
                  this.getDictionnaryTypes();
                })
                .catch(result => {
                  console.log("/dictype/"+row.id)
                  console.log(result);
                  this.$message({
                    message: '系统繁忙，稍后重试！',
                    type: 'error'
                  });
                });
          })
          .catch(()=>{//取消
          });
    },

    //显示编辑界面
    handleEdit: function (index, row) {
      this.myTitles = "编辑";
      this.editFormVisible = true;
      //克隆一份当前行的数据到新的集合中,然后赋值给修改表单,修改表单的输入行prop属性就是行
      this.editForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function () {
      this.myTitles = "新增";
      this.editFormVisible = true;
      this.editForm = {//新增前需要重置表单数据
        id:null,
        sn:"",
        name: ""
      };
    },
    //5.新增与编辑--这里的新增与编辑只针对前三个基本数据变量有用,剩下的管理员与上级部门都涉及引用数据部分,要从domain进行涉及
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //5.1开始修改,从模型层克隆拿到数据,然后封装到一个新的空对象中,传回后端
            //let para = Object.assign({}, this.xxxx);
            let para = Object.assign({}, this.editForm);

            this.$http.put("/dictype",para)
                .then(result => {
                  result = result.data;//这里会拿到AjaxResult
                  if(!result.success){
                    this.$message({
                      message: result.message,
                      type: 'error'
                    });
                  }else{//成功才关闭模态框
                    this.$message({
                      message: result.message,
                      type: 'success'
                    });
                    this.editFormVisible = false;//关闭模态框
                    this.editLoading = false;
                  }
                  //5.3 删除以后定位到第一页,定位以后再执行查询,吧数据展示出来
                  this.currentPage = 1;
                  this.getDictionnaryTypes();
                })
                .catch(result => {
                  console.log("/dictype/"+row.id)
                  console.log(result);
                  this.$message({
                    message: '系统繁忙，稍后重试！',
                    type: 'error'
                  });
                });
          })
        }
      })
    },

    //4.批量删除
    selsChange: function (sels) {
      //4.1先拿到选中当前行的数据,返回模型层,
      this.sels = sels;
    },
    batchRemove: function () {
      //4.2这里通过map函数获取到所有选中行的id,返回类型是一个数组...[1,2,3]
      //item是循环的固定写法
      var ids = this.sels.map(item => item.id);
      console.log(ids)
      this.$confirm('这会导致明细表中在该产品编号下的所有明细一起删除,确认删除选中记录吗?', '提示', {
        type: 'warning'
      }).then(() => {//确定下一步
        this.$http.patch("/dictype",ids)
            .then(result => {
              this.listLoading = true;
              result = result.data;//这里会拿到AjaxResult
              if(!result.success){
                this.$message({
                  //3.2在工具类的ajaxresult中已经配置了信息,这里直接用
                  message: result.message,
                  type: 'error'
                });
              }
              //3.3 删除以后定位到第一页,定位以后再执行查询,吧数据展示出来
              this.listLoading = false;
              this.currentPage = 1;
              this.getDictionnaryTypes();
            })
            .catch(result => {
              console.log("/dictype/"+row.id)
              console.log(result);
              this.$message({
                message: '系统繁忙，稍后重试！',
                type: 'error'
              });
            });
      })
          .catch(() => {

          });
    },


  },
  mounted() {
    this.getDictionnaryTypes();
  }
}

</script>

<style scoped>

</style>